﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Project_ScansiaPacific
{
    public partial class FrEditXK : DevComponents.DotNetBar.Office2007Form
    {
        public FrEditXK()
        {
            InitializeComponent();
        }
        private string  NguonCungCap1, GhiChu1,Add_check,AmDo1,child=" - ",ChungTu1,Users,TenNguon1,DVT1="";
        private double SoKhoi1, TongKhoi1;
        private int STT1,Serial1;
        private float ChieuDay1, ChieuRong1, ChieuDai1, SoLuong1;
        public void User(string s, string Admin)
        {
            lb_User.Text = s;
            if (Admin == "vuthaihuynh")
            {
                bt_insert.Enabled = true;
                bt_save.Enabled = true;
                bt_delete.Enabled = true;
            }
            else
            {
                bt_insert.Enabled = false;
                bt_save.Enabled = false;
                bt_delete.Enabled = false;
            }
        }
        private void combo_load()
        {
            FormProcess Frp = new FormProcess();
            string sql_NCC = @"select MaNCC,TenNCC from dbo.NhaCungCap";
            Frp.cb1(cb_NguonCungCap, sql_NCC, 0, 1);
        }
        public void ChungTu_Xuat(string s)
        {
            lb_ChungTu.Text = s;
        }
        public void serial(string s)
        {
            lb_serial.Text = s;
        }
        public void datagrid_load(DataSet ds)
        {
            try
            {
                dataGridView1.Columns["ChieuDay"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;//Hien thi text ben lề phải của gridview
                dataGridView1.Columns["ChieuRong"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
                dataGridView1.Columns["ChieuDai"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
                dataGridView1.Columns["SoLuong"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
                dataGridView1.Columns["SoKhoi"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    ChieuDay1 = float.Parse(ds.Tables[0].Rows[i]["ChieuDay"].ToString());
                    ChieuRong1 = float.Parse(ds.Tables[0].Rows[i]["ChieuRong"].ToString());
                    ChieuDai1 = float.Parse(ds.Tables[0].Rows[i]["ChieuDai"].ToString());
                    SoLuong1 = float.Parse(ds.Tables[0].Rows[i]["SoLuong"].ToString());
                    GhiChu1 = ds.Tables[0].Rows[i]["GhiChu"].ToString();
                    Serial1 = int.Parse(ds.Tables[0].Rows[i]["Serial_XK"].ToString());
                    SoKhoi1 = double.Parse(ds.Tables[0].Rows[i]["SoKhoi"].ToString());
                    TongKhoi1 += SoKhoi1;
                    lb_TongKhoi.Text = string.Format("{0:###,###.#########}", TongKhoi1);
                    TenNguon1 = ds.Tables[0].Rows[i]["TenNguon"].ToString();
                    Add_check = "";
                    NguonCungCap1 = ds.Tables[0].Rows[i]["NguonCungCap"].ToString();
                    AmDo1 = ds.Tables[0].Rows[i]["AmDo"].ToString();
                    STT1 = int.Parse(ds.Tables[0].Rows[i]["STT"].ToString());
                    dataGridView1.Rows.Add(ChieuDay1, ChieuRong1, ChieuDai1, SoLuong1, SoKhoi1, NguonCungCap1,TenNguon1,AmDo1, GhiChu1, Add_check, Serial1, STT1);
                }
                /*DateTime dt_CT = DateTime.Parse(ds.Tables[0].Rows[0]["Ngay"].ToString());
                DateTime today = DateTime.Today;
                int check = today.Subtract(dt_CT).Days;
                if (check == 0)
                {
                    bt_insert.Visible = true;
                    bt_save.Visible = true;
                    bt_delete.Visible = true;
                }*/
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        public void visible_NguonCC(int i)
        {
            try
            {
                Process prs = new Process();
                string select_LyDoXuat = @"select LyDoXuat from dbo.Serial_XKGo where Serial_XK=" + i;
                SqlConnection Conn = prs.Connect();
                SqlCommand cmd = new SqlCommand();
                Conn.Open();
                cmd.Connection = Conn;
                cmd.CommandText = select_LyDoXuat;
                string LyDo = cmd.ExecuteScalar().ToString();
                if (LyDo == "X001" || LyDo == "X003")
                    cb_NguonCungCap.Enabled = true;
                else
                    cb_NguonCungCap.Enabled = false;
                Conn.Close();
                cmd.Dispose();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            

        }

        private void FrEditXK_Load(object sender, EventArgs e)
        {
            FormProcess Frp = new FormProcess();
            combo_load();
        }

        private void txt_ChieuDay_TextChanged(object sender, EventArgs e)
        {
            FormProcess Frp = new FormProcess();
            Frp.checktext_number(txt_ChieuDay);
        }

        private void txt_ChieuRong_TextChanged(object sender, EventArgs e)
        {
            FormProcess Frp = new FormProcess();
            Frp.checktext_number(txt_ChieuRong);
        }

        private void txt_ChieuDai_TextChanged(object sender, EventArgs e)
        {
            FormProcess Frp = new FormProcess();
            Frp.checktext_number(txt_ChieuDai);
        }

        private void txt_SoLuong_TextChanged(object sender, EventArgs e)
        {
            FormProcess Frp = new FormProcess();
            Frp.checktext_number(txt_SoLuong);
        }
        private void insert_gridview()
        {
            Process prs = new Process();
            FormProcess Frp = new FormProcess();
            try
            {
                if (txt_ChieuDai.Text == "" || txt_ChieuDay.Text == "" || txt_ChieuRong.Text == "" || txt_SoLuong.Text == "")
                    MessageBox.Show("Bạn nhập thiếu thông tin");
                else
                {
                    Serial1 = int.Parse(dataGridView1.Rows[0].Cells["Serial_XK"].Value.ToString());
                    //Serial1 = 0;
                    GhiChu1 = txt_GhiChu.Text;
                    AmDo1 = txt_AmDo.Text;
                    if (cb_NguonCungCap.Text != "")
                    {
                        NguonCungCap1 = prs.substring_left(cb_NguonCungCap.Text, child);
                        TenNguon1 = prs.substring_right(cb_NguonCungCap.Text, child);
                        if (NguonCungCap1 == "")
                            MessageBox.Show("Bạn Cần nhập thông tin trong ComboBox");
                    }
                    if (cb_NguonCungCap.Text == "")
                    {
                        NguonCungCap1 = cb_NguonCungCap.Text;
                        TenNguon1 = cb_NguonCungCap.Text;
                    }

                    if (Frp.isfloat(txt_ChieuDay.Text))
                        ChieuDay1 = float.Parse(txt_ChieuDay.Text);
                    else
                        ChieuDay1 = 0;
                    if (Frp.isfloat(txt_ChieuRong.Text))
                        ChieuRong1 = float.Parse(txt_ChieuRong.Text);
                    else
                        ChieuRong1 = 0;
                    if (Frp.isfloat(txt_ChieuDai.Text))
                        ChieuDai1 = float.Parse(txt_ChieuDai.Text);
                    else
                        ChieuDai1 = 0;
                    if (Frp.isfloat(txt_SoLuong.Text))
                        SoLuong1 = float.Parse(txt_SoLuong.Text);
                    else
                        SoLuong1 = 0;
                    SoKhoi1 = (ChieuRong1 * ChieuDai1 * ChieuDay1 * SoLuong1) / 1000000000;
                    dataGridView1.Rows.Add(ChieuDay1, ChieuRong1, ChieuDai1, SoLuong1, SoKhoi1, NguonCungCap1,TenNguon1, AmDo1, GhiChu1, "1", Serial1);
                    TongKhoi1 = double.Parse(lb_TongKhoi.Text);
                    TongKhoi1 += SoKhoi1;
                    lb_TongKhoi.Text = TongKhoi1.ToString();
                    txt_SoLuong.Text = "";
                    txt_ChieuDay.Text = "";
                    txt_ChieuRong.Text = "";
                    txt_ChieuDai.Text = "";
                    txt_GhiChu.Text = "";
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void bt_insert_Click(object sender, EventArgs e)
        {
            insert_gridview();
        }

        private void bt_delete_Click(object sender, EventArgs e)
        {
            try
            {
                Process prs = new Process();
                SqlConnection Conn = prs.Connect();
                Conn.Open();
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = Conn;
                int i = dataGridView1.CurrentCell.RowIndex;
                ChungTu1 = lb_ChungTu.Text;
                SoKhoi1 = double.Parse(dataGridView1.Rows[i].Cells["SoKhoi"].Value.ToString());
                STT1 = int.Parse(dataGridView1.Rows[i].Cells["STT"].Value.ToString());
                //int STT_del = int.Parse(STT1);
                //if (rd_HHX.Checked == true)
                {
                    DialogResult d = MessageBox.Show("Bạn có chắc muốn xóa dòng trong chứng từ " + ChungTu1 + "\nVới số Khối là " + SoKhoi1, "Thông Báo", MessageBoxButtons.OKCancel);
                    if (d == DialogResult.OK)
                    {
                        try
                        {
                            string sql_del = @"delete from dbo.XuatKhoGo where STT=" + STT1;
                            cmd.CommandText = sql_del;
                            cmd.ExecuteNonQuery();
                            TongKhoi1 = double.Parse(lb_TongKhoi.Text);
                            TongKhoi1 = TongKhoi1 - SoKhoi1;
                            lb_TongKhoi.Text = TongKhoi1.ToString();
                            dataGridView1.Rows.RemoveAt(i);
                            //MessageBox.Show("Dữ li trên đã được xóa");
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show("Đã xảy ra lỗi khi delete, bạn vui lòng kiểm tra lại \n" + ex.Message);
                        }
                    }

                }
                cmd.Dispose();
                Conn.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void Save()
        {
            int i;
            try
            {
                Data dta = new Data();
                SqlConnection Conn = dta.Connect();
                Conn.Open();
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = Conn;
                for (i = dataGridView1.Rows.Count - 2; i > 0; i--)
                {
                    if (dataGridView1.Rows[i].Cells["Add"].Value.ToString() == "1")
                    {
                        ChieuRong1 = float.Parse(dataGridView1.Rows[i].Cells["ChieuRong"].Value.ToString());
                        ChieuDai1 = float.Parse(dataGridView1.Rows[i].Cells["ChieuDai"].Value.ToString());
                        ChieuDay1 = float.Parse(dataGridView1.Rows[i].Cells["ChieuDay"].Value.ToString());
                        SoLuong1 = float.Parse(dataGridView1.Rows[i].Cells["SoLuong"].Value.ToString());
                        SoKhoi1 = double.Parse(dataGridView1.Rows[i].Cells["SoKhoi"].Value.ToString());
                        //DVT1 = dataGridView1.Rows[i].Cells["DVT"].Value.ToString();
                        NguonCungCap1 = dataGridView1.Rows[i].Cells["NguonCungCap"].Value.ToString();
                        TenNguon1 = dataGridView1.Rows[i].Cells["TenNguon"].Value.ToString();
                        GhiChu1 = dataGridView1.Rows[i].Cells["GhiChu"].Value.ToString();
                        Serial1 = int.Parse(lb_serial.Text);
                        ChungTu1 = lb_ChungTu.Text;
                        Users = lb_User.Text;
                        AmDo1 = txt_AmDo.Text;
                        string insert_NK = @"insert into dbo.XuatKhoGo(Serial_XK,ChungTu,NguonCungCap,ChieuDay,ChieuRong,ChieuDai,SoKhoi,SoLuong,DVT,GhiChu,UsersUpdate,TenNguon,AmDo) values(";
                        insert_NK += Serial1 + ",N'" + ChungTu1 + "',N'" + NguonCungCap1 + "'," + ChieuDay1 + "," + ChieuRong1 + "," + ChieuDai1 + "," + SoKhoi1 + "," + SoLuong1 + ",'" + DVT1 + "',N'" + GhiChu1 + "',N'" + Users + "',N'" + TenNguon1 + "',N'" + AmDo1+"')";
                        //MessageBox.Show(insert_NK);
                        cmd.CommandText = insert_NK;
                        cmd.ExecuteNonQuery();
                        dataGridView1.Rows.RemoveAt(i);
                    }
                }
                Conn.Close();
                cmd.Dispose();
                MessageBox.Show("Dữ liệu được lưu thành công");
                this.Close();
            }
            catch (Exception ex)
            {
                //string delete_CT = @"delete from dbo.HoaDon_KH where ChungTu=" + SoCT;
                //cmd.CommandText = delete_CT;
                //cmd.ExecuteNonQuery();
                MessageBox.Show("Dữ liệu thêm không thành công. Vui lòng kiểm tra lại \n " + ex.Message);
            }
        }

        private void bt_save_Click(object sender, EventArgs e)
        {
            Save();
        }

        private void dataGridView1_DoubleClick(object sender, EventArgs e)
        {
            if (dataGridView1.Rows.Count > 1)
            {
                try
                {

                    int r = dataGridView1.CurrentCell.RowIndex;
                    if (dataGridView1.Rows[r].Cells["Add"].Value.ToString() == "1")
                    {
                        TongKhoi1 = double.Parse(lb_TongKhoi.Text) - double.Parse(dataGridView1.Rows[r].Cells["SoKhoi"].Value.ToString());
                        lb_TongKhoi.Text = string.Format("{0:###,###.#########}", TongKhoi1);
                        dataGridView1.Rows.RemoveAt(r);
                        int j = dataGridView1.Rows.Count - 1;
                        dataGridView1.CurrentCell = dataGridView1.Rows[j].Cells["ChieuRong"];
                    }
                }
                catch (Exception ex)
                { MessageBox.Show(ex.Message); }
            }
            if (dataGridView1.Rows.Count == 1)
                lb_TongKhoi.Text = "0";
        }
        private void report()
        {
            Process prs = new Process();
            SqlConnection Conn = prs.Connect();
            SqlCommand cmd = new SqlCommand();
            DataSet ds = new DataSet();
            try
            {
                Conn.Open();
                string sql_select = @"select LyDoXuat from dbo.Serial_XKGo where Serial_XK=" + lb_serial.Text;
                cmd.Connection = Conn;
                cmd.CommandText = sql_select;
                Fr_Report FrR = new Fr_Report();
                string LyDoXuat1 = cmd.ExecuteScalar().ToString();
                if (LyDoXuat1 == "X001")
                {
                    sql_select = @"select Seri.Serial_XK,Seri.ChungTu,Seri.TKHaiQuan,Seri.LoaiGo,Seri.NoiNhan,Seri.KeHoach as MaKeHoach,Seri.TinhTrangGo,Seri.ADorKD,Seri.TieuChuan as FCS,Xuat.ChieuDay,Xuat.ChieuRong,Xuat.ChieuDai,Xuat.SoKhoi,Xuat.NguonCungCap,Xuat.SoLuong,Xuat.GhiChu as DiaChi,Xuat.AmDo
                                from dbo.Serial_XKGo as Seri inner join dbo.XuatKhoGo as Xuat on Seri.Serial_XK=Xuat.Serial_XK where Seri.Serial_XK=" + lb_serial.Text;

                    ds = prs.dts(sql_select);
                    FrR.report_XKSX(ds);
                    FrR.Show();

                }
                if (LyDoXuat1 == "X003")
                {
                    sql_select = @"select Seri.Serial_XK,Seri.ChungTu,Seri.TKHaiQuan,Seri.LoaiGo,Seri.NoiNhan,Seri.TinhTrangGo,Seri.ADorKD,Seri.TieuChuan as FCS,Xuat.ChieuDay,Xuat.ChieuRong,Xuat.ChieuDai,Xuat.SoKhoi,Xuat.NguonCungCap,Xuat.SoLuong,Xuat.GhiChu as DiaChi,Xuat.AmDo
                                from dbo.Serial_XKGo as Seri inner join dbo.XuatKhoGo as Xuat on Seri.Serial_XK=Xuat.Serial_XK where Seri.Serial_XK=" + lb_serial.Text;

                    ds = prs.dts(sql_select);
                    FrR.report_XKLS(ds);
                    FrR.Show();

                }
                if (LyDoXuat1 == "X004")
                {
                    sql_select = @"select Seri.Serial_XK,Seri.ChungTu,Seri.TKHaiQuan,Seri.LoaiGo,Seri.NoiNhan,Seri.TinhTrangGo,Seri.ADorKD,Seri.TieuChuan as FCS,Xuat.ChieuDay,Xuat.ChieuRong,Xuat.ChieuDai,Xuat.SoKhoi,Xuat.NguonCungCap,Xuat.SoLuong,Xuat.GhiChu as DiaChi,Xuat.AmDo
                                from dbo.Serial_XKGo as Seri inner join dbo.XuatKhoGo as Xuat on Seri.Serial_XK=Xuat.Serial_XK where Seri.Serial_XK=" + lb_serial.Text;

                    ds = prs.dts(sql_select);
                    FrR.report_XKBan(ds);
                    FrR.Show();
                }
                if (LyDoXuat1 == "X002")
                {
                    sql_select = @"select Seri.Serial_XK,Seri.ChungTu,Seri.TKHaiQuan,Seri.LoaiGo,Seri.NoiNhan,Seri.TinhTrangGo,Seri.ADorKD,Seri.TieuChuan as FCS,Xuat.ChieuDay,Xuat.ChieuRong,Xuat.ChieuDai,Xuat.SoKhoi,Xuat.NguonCungCap,Xuat.SoLuong,Xuat.GhiChu as DiaChi,Xuat.AmDo
                                from dbo.Serial_XKGo as Seri inner join dbo.XuatKhoGo as Xuat on Seri.Serial_XK=Xuat.Serial_XK where Seri.Serial_XK=" + lb_serial.Text;

                    ds = prs.dts(sql_select);
                    FrR.report_XKGC(ds);
                    FrR.Show();
                }
                ds.Dispose();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void bt_report_Click(object sender, EventArgs e)
        {
            report();
        }
       
    }
}
